// 增强版知识库，支持更多车机相关的问答
export interface KnowledgeItem {
  keywords: string[];
  response: string;
  category: string;
  priority: number;
  action?: string;
  parameters?: Record<string, any>;
}

export class EnhancedKnowledgeBase {
  private knowledgeItems: KnowledgeItem[] = [
    // 空调相关
    {
      keywords: ['空调', '制冷', '制热', '温度', '冷气', '暖气'],
      response: '您想要调节空调吗？我可以帮您开启/关闭空调、调节温度、设置风速等。',
      category: 'air_conditioner',
      priority: 10,
      action: 'show_ac_panel'
    },
    {
      keywords: ['太热', '很热', '热死了', '降温'],
      response: '我来帮您降低温度。正在将空调调至22度并开启制冷模式。',
      category: 'air_conditioner',
      priority: 9,
      action: 'cool_down'
    },
    {
      keywords: ['太冷', '很冷', '冷死了', '升温'],
      response: '我来帮您升高温度。正在将空调调至26度并开启制热模式。',
      category: 'air_conditioner',
      priority: 9,
      action: 'warm_up'
    },
    {
      keywords: ['风速', '风力', '风量', '吹风'],
      response: '我可以帮您调节风速。您希望设置为几档？（1-7档）',
      category: 'air_conditioner',
      priority: 8
    },
    
    // 音乐相关
    {
      keywords: ['音乐', '歌曲', '播放', '暂停', '唱歌'],
      response: '我可以帮您控制音乐播放。您想要播放、暂停、切换歌曲还是调节音量？',
      category: 'music',
      priority: 10,
      action: 'show_music_panel'
    },
    {
      keywords: ['下一首', '切歌', '换歌', '跳过'],
      response: '正在为您播放下一首歌曲。',
      category: 'music',
      priority: 9,
      action: 'next_song'
    },
    {
      keywords: ['上一首', '前一首', '返回'],
      response: '正在为您播放上一首歌曲。',
      category: 'music',
      priority: 9,
      action: 'previous_song'
    },
    {
      keywords: ['音量', '声音', '大声点', '小声点'],
      response: '我可以帮您调节音量。您希望调高还是调低？',
      category: 'music',
      priority: 8
    },
    {
      keywords: ['播放列表', '歌单', '收藏'],
      response: '您想要查看播放列表、创建歌单还是收藏当前歌曲？',
      category: 'music',
      priority: 7,
      action: 'show_playlist'
    },
    
    // 导航相关
    {
      keywords: ['导航', '地图', '路线', '目的地', '去哪里'],
      response: '我可以帮您规划路线。请告诉我您想去哪里？',
      category: 'navigation',
      priority: 10,
      action: 'show_navigation_panel'
    },
    {
      keywords: ['回家', '到家', '家里'],
      response: '正在为您规划回家路线。',
      category: 'navigation',
      priority: 9,
      action: 'navigate_home'
    },
    {
      keywords: ['公司', '上班', '工作'],
      response: '正在为您规划到公司的路线。',
      category: 'navigation',
      priority: 9,
      action: 'navigate_work'
    },
    {
      keywords: ['加油站', '加油', '油站'],
      response: '正在为您寻找附近的加油站。',
      category: 'navigation',
      priority: 8,
      action: 'find_gas_station'
    },
    {
      keywords: ['停车场', '停车', '车位'],
      response: '正在为您寻找附近的停车场。',
      category: 'navigation',
      priority: 8,
      action: 'find_parking'
    },
    {
      keywords: ['堵车', '拥堵', '路况'],
      response: '正在为您查询实时路况，寻找最佳路线。',
      category: 'navigation',
      priority: 7,
      action: 'check_traffic'
    },
    
    // 车辆状态相关
    {
      keywords: ['车辆状态', '车况', '检查', '诊断'],
      response: '正在为您检查车辆状态...',
      category: 'vehicle_status',
      priority: 10,
      action: 'show_vehicle_status'
    },
    {
      keywords: ['油量', '汽油', '燃油', '没油'],
      response: '当前油量充足，剩余85%。如需加油，我可以为您导航到最近的加油站。',
      category: 'vehicle_status',
      priority: 9
    },
    {
      keywords: ['电瓶', '电池', '电量'],
      response: '电瓶状态良好，电压12.6V。',
      category: 'vehicle_status',
      priority: 8
    },
    {
      keywords: ['轮胎', '胎压'],
      response: '轮胎状态正常，胎压在正常范围内。',
      category: 'vehicle_status',
      priority: 8
    },
    {
      keywords: ['发动机', '引擎', '马达'],
      response: '发动机运行正常，温度90°C，处于正常范围。',
      category: 'vehicle_status',
      priority: 8
    },
    
    // 车门车窗相关
    {
      keywords: ['车门', '开门', '关门', '锁门'],
      response: '我可以帮您控制车门。您要锁门还是解锁？',
      category: 'vehicle_control',
      priority: 9,
      action: 'door_control'
    },
    {
      keywords: ['车窗', '开窗', '关窗', '升降'],
      response: '我可以帮您控制车窗。您要开窗还是关窗？',
      category: 'vehicle_control',
      priority: 9,
      action: 'window_control'
    },
    {
      keywords: ['天窗', '阳光'],
      response: '我可以帮您控制天窗。您要打开还是关闭天窗？',
      category: 'vehicle_control',
      priority: 8,
      action: 'sunroof_control'
    },
    
    // 灯光相关
    {
      keywords: ['打开大灯', '开启大灯', '打开前灯'],
      response: '好的，正在为您打开大灯。',
      category: 'vehicle_control',
      priority: 9,
      action: 'turn_on_headlights'
    },
    {
      keywords: ['关闭大灯', '关掉大灯', '关闭前灯'],
      response: '好的，大灯已关闭。',
      category: 'vehicle_control',
      priority: 9,
      action: 'turn_off_headlights'
    },
    {
      keywords: ['灯光', '大灯', '前灯', '照明'],
      response: '我可以帮您控制车灯。您要开启还是关闭大灯？',
      category: 'vehicle_control',
      priority: 8,
      action: 'light_control'
    },
    {
      keywords: ['雾灯', '雾天'],
      response: '正在为您开启雾灯。',
      category: 'vehicle_control',
      priority: 7,
      action: 'fog_light_on'
    },
    {
      keywords: ['双闪', '危险', '警示'],
      response: '正在为您开启危险警示灯。',
      category: 'vehicle_control',
      priority: 8,
      action: 'hazard_on'
    },
    
    // 雨刷控制
    {
      keywords: ['打开雨刷', '开启雨刷', '下雨了'],
      response: '好的，正在为您打开雨刷。',
      category: 'vehicle_control',
      priority: 9,
      action: 'turn_on_wipers'
    },
    {
      keywords: ['关闭雨刷', '关掉雨刷', '雨停了'],
      response: '雨刷已关闭。',
      category: 'vehicle_control',
      priority: 9,
      action: 'turn_off_wipers'
    },
    
    // 环境信息相关
    {
      keywords: ['天气', '温度', '气温', '外面'],
      response: '当前外界温度18°C，天气晴朗。',
      category: 'environment',
      priority: 7
    },
    {
      keywords: ['时间', '几点', '现在'],
      response: `现在是${new Date().toLocaleTimeString()}。`,
      category: 'environment',
      priority: 6
    },
    {
      keywords: ['日期', '今天', '几号'],
      response: `今天是${new Date().toLocaleDateString()}。`,
      category: 'environment',
      priority: 6
    },
    
    // 舒适性功能
    {
      keywords: ['座椅向前', '座位向前', '往前调座椅'],
      response: '好的，正在为您向前调节座椅。',
      category: 'comfort',
      priority: 8,
      action: 'seat_position_forward',
      parameters: { seat: 'driver' }
    },
    {
      keywords: ['座椅向后', '座位向后', '往后调座椅'],
      response: '好的，正在为您向后调节座椅。',
      category: 'comfort',
      priority: 8,
      action: 'seat_position_backward',
      parameters: { seat: 'driver' }
    },
    {
      keywords: ['座椅', '座位', '加热', '按摩'],
      response: '您想要调节座椅位置、开启座椅加热还是按摩功能？',
      category: 'comfort',
      priority: 7,
      action: 'seat_control'
    },
    {
      keywords: ['方向盘', '转向'],
      response: '您想要调节方向盘位置还是开启方向盘加热？',
      category: 'comfort',
      priority: 6,
      action: 'steering_control'
    },
    {
      keywords: ['后视镜', '镜子'],
      response: '我可以帮您调节后视镜角度。',
      category: 'comfort',
      priority: 6,
      action: 'mirror_control'
    },
    
    // 通信相关
    {
      keywords: ['电话', '打电话', '通话', '联系'],
      response: '您想要拨打电话吗？请告诉我联系人姓名或号码。',
      category: 'communication',
      priority: 8,
      action: 'make_call'
    },
    {
      keywords: ['短信', '信息', '消息'],
      response: '您想要发送短信吗？请告诉我收件人和内容。',
      category: 'communication',
      priority: 7,
      action: 'send_message'
    },
    {
      keywords: ['蓝牙', '连接', '配对'],
      response: '您想要连接蓝牙设备吗？我可以帮您搜索和配对设备。',
      category: 'communication',
      priority: 7,
      action: 'bluetooth_settings'
    },
    
    // 娱乐相关
    {
      keywords: ['电台', '广播', '新闻'],
      response: '您想要收听电台吗？我可以为您搜索本地电台。',
      category: 'entertainment',
      priority: 6,
      action: 'show_radio'
    },
    {
      keywords: ['视频', '电影', '电视'],
      response: '为了您的安全，建议在停车时观看视频内容。',
      category: 'entertainment',
      priority: 5
    },
    {
      keywords: ['游戏', '娱乐'],
      response: '为了您的安全，建议在停车时使用娱乐功能。',
      category: 'entertainment',
      priority: 5
    },
    
    // 设置相关
    {
      keywords: ['设置', '配置', '调整', '修改'],
      response: '您想要修改哪些设置？我可以帮您调整语音、显示、车机等各项设置。',
      category: 'settings',
      priority: 7,
      action: 'show_settings'
    },
    {
      keywords: ['语音设置', '语音配置'],
      response: '您想要调整语音识别灵敏度、音量还是更换语音？',
      category: 'settings',
      priority: 6,
      action: 'voice_settings'
    },
    {
      keywords: ['显示设置', '屏幕设置'],
      response: '您想要调整屏幕亮度、主题还是字体大小？',
      category: 'settings',
      priority: 6,
      action: 'display_settings'
    },
    
    // 帮助相关
    {
      keywords: ['帮助', '功能', '使用', '怎么用'],
      response: '我是您的智能车机助手，可以帮您控制空调、音乐、导航等功能。您可以直接说出需求，比如"打开空调"、"播放音乐"、"导航到北京"等。',
      category: 'help',
      priority: 10
    },
    {
      keywords: ['说明', '教程', '指南'],
      response: '语音控制非常简单：说出"小助手"唤醒我，然后直接说出您的需求即可。支持的功能包括空调控制、音乐播放、导航、车辆状态查询等。',
      category: 'help',
      priority: 8
    },
    
    // 安全相关
    {
      keywords: ['紧急', '急救', '救命', '报警'],
      response: '检测到紧急情况，正在为您拨打紧急联系人。如需报警请说"报警"。',
      category: 'emergency',
      priority: 15,
      action: 'emergency_call'
    },
    {
      keywords: ['报警', '110', '警察'],
      response: '正在为您拨打110报警电话。',
      category: 'emergency',
      priority: 15,
      action: 'call_police'
    },
    {
      keywords: ['火警', '119', '消防'],
      response: '正在为您拨打119火警电话。',
      category: 'emergency',
      priority: 15,
      action: 'call_fire'
    },
    {
      keywords: ['急救', '120', '医院'],
      response: '正在为您拨打120急救电话。',
      category: 'emergency',
      priority: 15,
      action: 'call_ambulance'
    },
    
    // 日常聊天
    {
      keywords: ['你好', '早上好', '下午好', '晚上好'],
      response: '您好！我是您的智能车机助手，很高兴为您服务！',
      category: 'greeting',
      priority: 5
    },
    {
      keywords: ['再见', '拜拜', '回见'],
      response: '再见！祝您一路平安！',
      category: 'greeting',
      priority: 5
    },
    {
      keywords: ['谢谢', '感谢'],
      response: '不用客气！很高兴能帮到您！',
      category: 'greeting',
      priority: 5
    },
    {
      keywords: ['今天怎么样', '心情', '状态'],
      response: '我状态很好，随时为您服务！希望您今天心情愉快！',
      category: 'chat',
      priority: 3
    }
  ];
  
  // 查找最佳匹配的回复
  findBestResponse(query: string): string | { response: string; action?: string; parameters?: Record<string, any> } {
    const lowerQuery = query.toLowerCase();
    let bestMatch: KnowledgeItem | null = null;
    let bestScore = 0;
    
    for (const item of this.knowledgeItems) {
      let score = 0;
      let matchCount = 0;
      
      // 计算关键词匹配度
      for (const keyword of item.keywords) {
        if (lowerQuery.includes(keyword)) {
          score += item.priority;
          matchCount++;
        }
      }
      
      // 如果有匹配的关键词，进一步计算匹配度
      if (matchCount > 0) {
        score = score * matchCount; // 匹配的关键词越多，分数越高
        
        if (score > bestScore) {
          bestScore = score;
          bestMatch = item;
        }
      }
    }
    
    if (bestMatch) {
      if (bestMatch.action) {
        return {
          response: bestMatch.response,
          action: bestMatch.action,
          parameters: bestMatch.parameters
        };
      } else {
        return bestMatch.response;
      }
    }
    
    // 如果没有匹配到，返回默认回复
    return '抱歉，我没有理解您的意思。您可以说"帮助"了解我的功能。';
  }
  
  // 根据分类获取相关知识
  getKnowledgeByCategory(category: string): KnowledgeItem[] {
    return this.knowledgeItems.filter(item => item.category === category);
  }
  
  // 获取所有分类
  getCategories(): string[] {
    const categories = new Set(this.knowledgeItems.map(item => item.category));
    return Array.from(categories);
  }
  
  // 添加新的知识项
  addKnowledgeItem(item: KnowledgeItem): void {
    this.knowledgeItems.push(item);
  }
  
  // 更新知识项
  updateKnowledgeItem(index: number, item: KnowledgeItem): void {
    if (index >= 0 && index < this.knowledgeItems.length) {
      this.knowledgeItems[index] = item;
    }
  }
  
  // 删除知识项
  removeKnowledgeItem(index: number): void {
    if (index >= 0 && index < this.knowledgeItems.length) {
      this.knowledgeItems.splice(index, 1);
    }
  }
  
  // 搜索知识项
  searchKnowledge(query: string): KnowledgeItem[] {
    const lowerQuery = query.toLowerCase();
    return this.knowledgeItems.filter(item => 
      item.keywords.some(keyword => keyword.includes(lowerQuery)) ||
      item.response.toLowerCase().includes(lowerQuery)
    );
  }
} 